package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.Star;
import com.njit.WeStudy.dataobject.Support;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface SupportMapper {
    @Delete({
        "delete from support",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into support (user_id, invitation_id,",
        "target_id, target_type, ",
        "is_supported)",
        "values ( #{userId,jdbcType=INTEGER}, #{invitationId,jdbcType=INTEGER},",
        "#{targetId,jdbcType=INTEGER}, #{targetType,jdbcType=VARCHAR}, ",
        "#{isSupported,jdbcType=BIT})"
    })
    int insert(Support record);

    @Select({
        "select",
        "id, user_id, invitation_id, target_id, target_type, is_supported",
        "from support",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results(id = "ResultMap",value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER),
        @Result(column="target_id", property="targetId", jdbcType=JdbcType.INTEGER),
        @Result(column="target_type", property="targetType", jdbcType=JdbcType.VARCHAR),
        @Result(column="is_supported", property="isSupported", jdbcType=JdbcType.BIT)
    })
    Support selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, user_id,invitation_id, target_id, target_type, is_supported",
        "from support"
    })
    @ResultMap("ResultMap")
    List<Support> selectAll();

    //查询当前层或用户的support
    @SelectProvider(type = QueryBuilder.class,method = "supportQueryByTargetId")
    @ResultMap("ResultMap")
    List<Support> supportQueryByTargetId(@Param("targetId") int targetId,@Param("targetType") String targetType);

    //查询当前用户对目标是否支持(加.getIsSupported())
    @SelectProvider(type = QueryBuilder.class,method = "QueryMyselfSupport")
    @ResultMap("ResultMap")
    Support QueryMyselfSupport(@Param("targetId") int targetId,@Param("targetType") String targetType,@Param("userId")int userId);

    //查看当前用户所有支持的用户或者跟帖的数量(加上.size())
    @SelectProvider(type = QueryBuilder.class,method = "supportQueryByUserId")
    @ResultMap("ResultMap")
    List<Support> supportQueryByUserId(@Param("userId")int userId, @Param("targetType")String targetType);

    //查看当前帖子的support
    @SelectProvider(type = QueryBuilder.class,method = "supportQueryByInvitationId")
    @ResultMap("ResultMap")
    List<Support> supportQueryByInvitationId(@Param("invitationId")int invitationId);

    //查看当前帖子的当前用户的support,用于分辨用户是否support某个storey或者comment
    @SelectProvider(type = QueryBuilder.class,method = "mySupportIdsQueryByInvitationId")
    @ResultMap("ResultMap")
    List<Support> mySupportIdsQueryByInvitationId(@Param("invitationId")int invitationId,@Param("userId")int userId);

    //查询用户对跟帖(其它用户)的support
    @UpdateProvider(type = UpdateBuilder.class,method = "updateSupportStatusByUserIdAndTargetId")
    int updateSupportStatusByUserIdAndTargetId(@Param("targetId")int targetId,@Param("targetType")String targetType,@Param("status")boolean status,@Param("userId")int userId);

    @Update({
        "update support",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "invitation_id = #{invitationId,jdbcType=INTEGER},",
          "target_id = #{targetId,jdbcType=INTEGER},",
          "target_type = #{targetType,jdbcType=VARCHAR},",
          "is_supported = #{isSupported,jdbcType=BIT}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Support record);
}